package servlet;
import DBCon.DBCon;
import bao.UserDao;
import model.User;
import service.UserService;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.*;
import java.sql.*;

public class Login_Servlet extends HttpServlet{
    public void init(ServletConfig config) throws ServletException{
        super.init(config);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        User user = new User();
        UserService service = new UserService();
        HttpSession session = req.getSession();
        String name = req.getParameter("name");
        String pwd = req.getParameter("password");
        user.setUser_name(name);
        user.setUser_password(pwd);
        try {
            if(service.login(user)){
                session.setAttribute("user_name",user.getReal_name());
                resp.sendRedirect("main/main.jsp");
            }else {
                session.setAttribute("msg","用户名或者密码错误！");
                resp.sendRedirect("login.jsp");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
